#!/usr/bin/env tclsh8.5
# $Id: test1.tcl 5f1b9d9a604f 2012/12/03 17:36:54 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# __version__  = "$Revision: 5f1b9d9a604f $"
# __author__   = "$Author: pgurumur $"
# __modified__ = "$Date: 2012-12-03 17:36:54Z $"

lappend ::auto_path venya
package require stdlib
package require tempfile

set progname [lindex [split $argv0 '/'] end]

proc optparse { argv argc } {
   global progname
   if {$argc <= 0} {
      usage 
   } else {
      array set args {}
      set count 0
      foreach element $argv {
         set args($count) $element
         incr count
      }
   
      stdlib::basename $progname
      tempfile::basename $progname

      set strsize ""
      set minsize 0
      set maxsize 0
      set unpack ""
      set packed ""
      set mask 0
      set wildcard 0
      set address ""
      set create 0
      set tempfile ""
      set delete 0
      set prime 0
      set isprime 0

      foreach element [array name args] {
         if {$args($element) == "-s" || $args($element) == "--strsize"} {
            set strsize $args([incr element])
         } elseif {$args($element) == "-m" || $args($element) == "--min"} {
            set minsize $args([incr element])
         } elseif {$args($element) == "-M" || $args($element) == "--max"} {
            set maxsize $args([incr element])
         } elseif {$args($element) == "-u" || $args($element) == "--unpack"} {
            set unpack $args([incr element])
         } elseif {$args($element) == "-p" || $args($element) == "--pack"} {
            set packed $args([incr element])
         } elseif {$args($element) == "-n" || $args($element) == "--netmask"} {
            set mask $args([incr element])
         } elseif {$args($element) == "-w" || $args($element) == "--wildcard"} {
            set mask $args([incr element])
            set wildcard 1
         } elseif {$args($element) == "-i" || $args($element) == "--ip"} {
            set address $args([incr element])
         } elseif {$args($element) == "-c" || $args($element) == "--create"} {
            set create 1
         } elseif {$args($element) == "-d" || $args($element) == "--delete"} {
            set tempfile [string trim $args([incr element])]
            set delete 1
         } elseif {$args($element) == "-h" || $args($element) == "--help"} {
            usage
         } elseif {$args($element) == "-P" || $args($element) == "--prime"} {
            set prime 1
         } elseif {$args($element) == "-I" || $args($element) == "--isprime"} {
            set isprime $args([incr element])
         }

      }

      if {[string length $strsize] > 0} {
         set randstr [stdlib::randstring $strsize]
         puts $randstr
      }

      if {$minsize && $maxsize} {
         if {$prime == 1} {
            set primelist [stdlib::getprime $maxsize $minsize]
            puts $primelist
         } else {
            set randint [stdlib::randint $minsize $maxsize]
            puts $randint
         }
      } elseif {$maxsize > 0} {
         if {$prime == 1} {
            set primelist [stdlib::getprime $maxsize 2]
            puts $primelist
         }
      }

      if {[string length $unpack] > 0} {
         set ipaddr [stdlib::pack2ip $unpack]
         puts $ipaddr
      }

      if {[string length $packed] > 0} {
         set ipaddr [stdlib::ip2pack $packed]
         puts $ipaddr
      }

      if {$mask > 0 } {
         set mask [stdlib::netmask $mask $wildcard]
         puts $mask
      }

      if {[string length $address] > 0} {
         array set addr [stdlib::netaddr $address]
         puts "$address details"
         puts "\t ip address: $addr(ip)"
         puts "\t netmask: $addr(netmask)"
         puts "\t subnet: $addr(mask)"
         puts "\t first usable ip: $addr(firstip)"
         puts "\t last usable ip: $addr(lastip)"
         puts "\t network: $addr(network)"
         puts "\t broadcast: $addr(broadcast)"
         puts "\t wildcard: $addr(wildcard)"
         puts "\t max ip addresses: $addr(maxip)"
         puts "\t max usable ip addresses: $addr(usableip)"
      }

      if {$create == 1} {
         set tempfile [tempfile::create ""]
         puts $tempfile
      }

      if {$delete == 1} {
         tempfile::destroy $tempfile
      }

      if {$isprime} {
         set retval [stdlib::isprime $isprime]
         if {$retval == 1} {
            puts "$isprime is a prime number"
         } else {
            puts "$isprime is a not prime number"
         }
      }
   }
}

proc usage {} {
   global progname
   puts "$progname options"
   puts "options: "
   puts "\t  -h | --help : prints this help"
   puts "\t  -s | --strsize : random string length size"
   puts "\t  -m | --min : minimum value of the random integer"
   puts "\t  -M | --max : maximum value of the random integer"
   puts "\t  -u | --unpack : unpack integer to ip address"
   puts "\t  -p | --pack : pack ip address to an integer"
   puts "\t  -n | --netmask : get netmask for values between 1 and 24"
   puts "\t  -w | --wildcard : get wildcard mask for values between 1 and 24"
   puts "\t  -i | --ip : get netaddr information for given subnet"
   puts "\t  -c | --create: Create tempfile "
   puts "\t  -d | --delete: delete tempfile "
   puts "\t  -P | --prime : Get prime number, use -m and -M to set the value"
   puts "\t  -I | --isprime : Check whether the number is prime"
   exit 1
}

optparse $argv $argc
